#include <bits/stdc++.h>
using namespace std;
const int N = 4000*100+10, P =  20071027, SS = 3e5+10;
int S, tr[N][26], sz, d[SS];
bool w[N];

void add(const string &s){
    int p = 0;
    for(char c:s){
        int& nx = tr[p][c-'a'];
        if(!nx) nx = ++sz;
        p = nx;
    }
    w[p] = true;
}


int solve(const string &s){
    d[0] = 1;
    for (int i = 1; i <= s.size(); ++i) {
        int p = 0;
        int sp = s.size()-i-1;
        for (int l = 1; l+sp < s.size(); ++l) {
            char c = s[l+sp];
            p = tr[p][c-'a'];
            if(!p) break;
            if(w[p]) d[i] = (d[i] + d[i-l])%P;
        }
    }
    return d[s.size()];
}

int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    string s; int T = 0;
    while (cin >> s){
        sz = 0; memset(tr, 0, sizeof tr);
        memset(d, 0, sizeof d);
        memset(w, false, sizeof w);
        cin >> S;
        for (int i = 0; i < S; ++i) {
            string in; cin >> in;
            add(in);
        }
        cout << "Case " << ++T << ": " << solve(s) << endl;
    }
    return 0;
}